package com.mostromas.ess.project.controller;

import com.mostromas.ess.entities.PDDProduct;
import com.mostromas.ess.entities.PDDProductCat;
import com.mostromas.ess.project.req.PDDProductREQ;
import com.mostromas.ess.project.req.ProjectCardNumberREQ;
import com.mostromas.ess.project.service.IPDDProductCatService;
import com.mostromas.ess.project.service.IPDDProductService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Api(value = "PDD商品接口", description = "提供拼多多商品的添加添加和查询")
@RestController
@RequestMapping("/product")
public class PDDProductController {

    @Autowired
    private IPDDProductService productService;

    @Autowired
    private IPDDProductCatService productCatService;

    @ApiOperation("根据条件查询商品列表")
    @PostMapping("/search")
    public Result search(@RequestBody PDDProductREQ req){
        return productService.queryList(req);
    }
    @ApiOperation("根据条件查询商品列表")
    @PostMapping
    public Result save(@RequestBody List<PDDProduct> list){
        return productService.saveList(list);
    }

    @ApiOperation("写入商品分类")
    @GetMapping("/saveCat")
    public Result saveCat() throws Exception {
        List<PDDProductCat> list = new ArrayList<>();
        getCats(0L, list);
        productCatService.saveList(list);
        return Result.ok();
    }

    @ApiOperation("根据关键词查询长尾词列表")
    @ApiImplicitParam(name = "keyword",value = "0",required = true)
    @GetMapping("/get_suggest/{keyword}")
    public Result searchSuggest(@PathVariable("keyword") String keyword) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String href = "https://api-shb1.pinduoduo.com/search_suggest?source=index&goods_id_list=&sug_srch_type=0&search_type=0&pdduid=5538915184902&query="+ URLEncoder.encode(keyword, "UTF-8");
        Request request = new Request.Builder()
                .url(href)
                .addHeader("content-type","application/json;charset=UTF-8")
                .addHeader("user-agent","android Mozilla/5.0 (Linux; Android 10; Pixel Build/QP1A.191005.007.A3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.186 Mobile Safari/537.36  phh_android_version/5.22.0 phh_android_build/c97637674e3be8f8b0df7e511b7077099b578ecf_pdd_patch phh_android_channel/main_guanwang pversion/863")
                .build();
        Response response = client.newCall(request).execute();
        if(!response.isSuccessful()){
            return Result.error(response.message());
        }
        String result = response.body().string();

        response.body().close();
        return Result.ok(result);
    }

    private void getCats(Long catId,List<PDDProductCat> list) throws Exception {
//        String clientId = "c42e057321fc49e1b4a3dbf9cfb4f7fd";
//        String clientSecret = "170951f04baad07583998dd67f50e0348b65ae21";
//        PopClient client = new PopHttpClient(clientId, clientSecret);
//        PddGoodsCatsGetRequest request = new PddGoodsCatsGetRequest();
//        request.setParentCatId(catId);
//        PddGoodsCatsGetResponse response = client.syncInvoke(request);
//        System.out.println(JsonUtil.transferToJson(response));
//        for (PddGoodsCatsGetResponse.GoodsCatsGetResponseGoodsCatsListItem gcr: response.getGoodsCatsGetResponse().getGoodsCatsList()) {
//            PDDProductCat ppc = new PDDProductCat();
//            ppc.setCatId(gcr.getCatId());
//            ppc.setLevel(gcr.getLevel());
//            ppc.setCatName(gcr.getCatName());
//            ppc.setParentCatId(gcr.getParentCatId());
//            list.add(ppc);
//            if(ppc.getLevel() < 4){
//                getCats(ppc.getCatId(),list);
//            }
//        }
    }
}
